<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Referee;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Admin\Renderable\UserTable;
use App\Models\User;
use App\Admin\Renderable\DealerTable;
use App\Models\Dealer;

class RefereeController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Referee(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('user_id')->modal('用户详情', function () {
                return app('App\Admin\Controllers\UserController')->current($this->user_id);
            });
            $grid->column('name');
            $grid->column('phone');
            $grid->column('province');
            $grid->column('city');
            $grid->column('dealer_id');
            $grid->column('interest');
            $grid->column('point_total');
            $grid->column('created_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('id')->integer()->width(2);
                $filter->equal('name')->width(2);
                $filter->equal('phone')->mobile()->width(2);
                $filter->equal('province')->width(2);
                $filter->equal('city')->width(2);
                $filter->equal('dealer_id', '经销商')->width(3)
                    ->selectTable(DealerTable::make(['id' => '']))
                    ->title('经销商列表')
                    ->dialogWidth('50%')
                    ->model(Dealer::class, 'id', 'name');
                $filter->equal('user_id', '用户')->width(2)
                    ->selectTable(UserTable::make(['id' => ''])) // 设置渲染类实例，并传递自定义参数
                    ->title('用户列表')
                    ->dialogWidth('50%') // 弹窗宽度，默认 800px
                    ->model(User::class, 'id', 'name'); // 设置编辑数据显示
                $filter->between('created_at')->datetime()->width(4);
            });
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
            $grid->scrollbarX();
            $grid->paginate(10);
            $grid->export()->csv();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Referee(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('name');
            $show->field('phone');
            $show->field('province');
            $show->field('city');
            $show->field('dealer_id');
            $show->field('interest');
            $show->field('point_total');
            $show->field('created_at');
            $show->field('updated_at');
            $show->disableDeleteButton();
            $show->disableListButton();
            $show->disableEditButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Referee(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('name');
            $form->text('phone');
            $form->text('province');
            $form->text('city');
            $form->text('dealer_id');
            $form->text('interest');
            $form->text('point_total');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
